package com.zhanghu.backendZray.controller.permission;

import com.zhanghu.backendZray.annotation.MenuNeed;
import com.zhanghu.backendZray.manager.role.UserRoleMenuManager;
import com.zhanghu.backendZray.pojo.dto.ApiResponse;
import com.zhanghu.backendZray.pojo.request.roleMenu.RoleMenuRequest;
import com.zhanghu.backendZray.pojo.vo.MenuVo;
import com.zhanghu.backendZray.service.RoleMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangdong
 * @since 2024-07-20
 */
@RestController
@RequestMapping("/roleMenu")
@RequiredArgsConstructor
public class RoleMenuController {
    private final RoleMenuService roleMenuService;
    private final UserRoleMenuManager userRoleMenuManager;

    /**
     * 获取全量菜单，并设置选中状态
     * @param roleId
     * @return
     */
    @MenuNeed(menuName = "role_manage")
    @GetMapping("getMenuListByRoleId")
    public ApiResponse<List<MenuVo>> getMenuListByRoleId(Integer roleId) {
        List<MenuVo> list = userRoleMenuManager.getMenuListByRoleId(roleId);
        return ApiResponse.ok(list);
    }

    /**
     * 只获取被选中子菜单的id集合
     * vue el-tree这边需要获取所有子节点id，不需要父节点。
     * @param roleId
     * @return
     */
    @MenuNeed(menuName = "role_manage")
    @GetMapping("getSelectedMenuListByRoleId")
    public ApiResponse<List<Integer>> getSelectedMenuListByRoleId(Integer roleId) {
        List<Integer> list = userRoleMenuManager.getSelectedMenuListByRoleId(roleId);
        return ApiResponse.ok(list);
    }

    /**
     * 角色菜单设置
     * @param roleMenuRequest
     * @return
     */
    @MenuNeed(menuName = "role_manage")
    @PostMapping("roleMenuSetting")
    public ApiResponse roleMenuSetting(@RequestBody RoleMenuRequest roleMenuRequest) {
        userRoleMenuManager.roleMenuSetting(roleMenuRequest);
        return ApiResponse.ok();
    }
}
